package com.hkts.management.cargolabel.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.cargolabel.entity.HktsCargoLabel;
import com.hkts.management.cargolabel.service.IHktsCargoLabelService;
import com.hkts.management.cargolabel.mapper.HktsCargoLabelMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hml
 * @since 2021-04-22
 */
@Service
@Transactional
public class HktsCargoLabelServiceImpl extends ServiceImpl<HktsCargoLabelMapper, HktsCargoLabel> implements IHktsCargoLabelService {


    @Autowired
    private HktsCargoLabelMapper hktsCargoLabelMapper;

    @Override
    public IPage<HktsCargoLabel> selectPage(Page<HktsCargoLabel> page, HktsCargoLabel hktsCargoLabel) {
        QueryWrapper<HktsCargoLabel> query = Wrappers.query();
        QueryWrapper<HktsCargoLabel> entityWrapper = getEntityWrapper(query, hktsCargoLabel);
        return hktsCargoLabelMapper.selectPage(page, entityWrapper);
    }


    @Override
    public List<HktsCargoLabel> selectList(HktsCargoLabel hktsCargoLabel) {
        QueryWrapper<HktsCargoLabel> query = Wrappers.query();
        QueryWrapper<HktsCargoLabel> entityWrapper = getEntityWrapper(query, hktsCargoLabel);
        return hktsCargoLabelMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsCargoLabel> getEntityWrapper(QueryWrapper<HktsCargoLabel> entityWrapper, HktsCargoLabel hktsCargoLabel) {
        //条件拼接
        /*if (StringUtils.isNotBlank(hktsCargoLabel.getLabelName())) {
            entityWrapper.lambda().like(hktsCargoLabel::getLabelName,hktsCargoLabel.getLabelName());
        }*/
        return entityWrapper;
    }
    /**
     * 根据货源id查询标签
     */
    public List<String> getCargoLanelByCargoId(String cargoId,Integer labelType){
        QueryWrapper<HktsCargoLabel> query = Wrappers.query();
        query.lambda().eq(HktsCargoLabel::getCargoId,cargoId)
                .eq(labelType!=null,HktsCargoLabel::getLabelType,labelType);
        List<HktsCargoLabel> hktsCargoLabels=hktsCargoLabelMapper.selectList(query);
        List<String> labels=hktsCargoLabels.stream().map(t->t.getLabelName()).collect(Collectors.toList());
        return labels;
    }


}
